package cn.ffcs.unm.cost.action;

import java.text.SimpleDateFormat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.ListModelList;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.unm.cost.action.bean.TaxGroupMainBean;
import cn.ffcs.unm.cost.manager.CostManager;
import cn.ffcs.unm.cost.model.TaxGroup;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.NumericUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Controller
@Scope("prototype")
public class TaxGroupMainComposer extends BasePortletComposer {

	private static final long serialVersionUID = 576517743853666451L;

	private TaxGroupMainBean bean = new TaxGroupMainBean();
	/**
	 * 查询实体
	 */
	private TaxGroup qryTaxGroup;

	@SuppressWarnings("unused")
	private TaxGroup taxGroup;

	@Autowired
	@Qualifier("costManager")
	private CostManager costManager;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化
	 * 
	 * @throws Exception
	 */
	public void onCreate$taxGroupMainWin() throws Exception {
		//this.onQuery();
	}

	public void onQuery() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		if (!StrUtil.isNullOrEmpty(bean.getMonthId().getValue())) {
			String monthId = formatter.format(bean.getMonthId().getValue());
			clearVariable();
			TaxGroup objTaxGroup = TaxGroup.newInstance();
			
			String cGroupName = bean.getCGroupName().getValue();
			
			//PubUtil.fillPoFromBean(bean, objTaxGroup);
			qryTaxGroup = objTaxGroup;
			int index = bean.getRadiogroup().getSelectedIndex();
			if (index == 0) {
				String monthIdMonth = monthId.substring(0, 6);
				qryTaxGroup.setMonthId(monthIdMonth);
			}else {
				qryTaxGroup.setMonthId(monthId);
			}
			qryTaxGroup.setCGroupName(cGroupName);
			this.bean.getTaxGroupListboxPaging().setActivePage(0);
			onTaxGroupQuery();
		}else {
			ZkUtil.showError("请输入账期", "温馨提示");
		}
	}

	public void onReset() {
		bean.getCGroupName().setValue(null);
		bean.getMonthId().setValue(null);
		clearVariable();
		ListboxUtils.clearListbox(this.bean.getTaxGroupListbox());
		this.bean.getTaxGroupListboxPaging().setTotalSize(1);
	}

	public void onTaxGroupQuery() {
		if (this.qryTaxGroup != null) {
			int activePage = bean.getTaxGroupListboxPaging().getActivePage();
			int pageSize = bean.getTaxGroupListboxPaging().getPageSize();
			PageInfo pageInfo = costManager.queryPageInfoByTaxGroup(
					qryTaxGroup, activePage + 1, pageSize);
			ListModelList dataList = new BindingListModelList(
					pageInfo.getDataList(), true);
			bean.getTaxGroupListbox().setModel(dataList);
			bean.getTaxGroupListboxPaging().setTotalSize(
					NumericUtil.nullToZero(pageInfo.getTotalCount()));
		}
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		qryTaxGroup = null;
		taxGroup = null;
	}

	/**
	 * 翻页
	 */
	public void onListboxPaging() {
		this.onTaxGroupQuery();
	}

}
